<?php

/*
	Class: Locale

	About: Author
		Rich Joslin

	About: License
		<http://communit.as/docs/license>

	About: See Also
		<Cts_Controller_Action_Abstract>

*/
class LocaleController extends Cts_Controller_Action_Abstract {

	/* Group: Actions */

	/*
		Function: choose
	*/
	function chooseAction() {
		// TODO : if there is a cookie set already, redirect - unless a certain flag is present
		if (!$this->_request->has('change') && $this->_request->has('locale_code')) {
			$this->_redirect("/".$this->_request->get('locale_code'));
		}
		// TODO : get the list of available countries and languages from a data source
		$countries = array();
		$countries["US"] = array("name" => "United States", "code" => "US");
		$countries["FR"] = array("name" => "France", "code" => "FR");
		$countries["UK"] = array("name" => "United Kingdom", "code" => "UK");
		$countries["DE"] = array("name" => "Deutschland", "code" => "DE");
		$countries["JP"] = array("name" => "日本", "code" => "JP");
		foreach ($countries as $country) {
			$country["languages"] = array();
		}
		$countries["US"]["languages"]["en"] = array("name" => "English", "code" => "en");
		$countries["US"]["languages"]["es"] = array("name" => "Español", "code" => "es");
		$countries["FR"]["languages"]["fr"] = array("name" => "Français", "code" => "fr");
		$countries["UK"]["languages"]["en"] = array("name" => "English", "code" => "en");
		$countries["DE"]["languages"]["de"] = array("name" => "Deutsch", "code" => "de");
		$countries["JP"]["languages"]["jp"] = array("name" => "日本語", "code" => "jp");
		$this->view->countries = $countries;
	}

	/*
		Function: setcookie
	*/
	function setcookieAction() {
		$request = new Cts_Request($this->getRequest());
		if ($request->has("code")) {
			$locale_code = $request->code;
			if (Cts_Translate::validateLocaleCode($locale_code)) {
				setcookie("locale_code", $locale_code, null, "/");
				if ($request->has("return_url")) {
					$this->_redirect($request->return_url);
				} else {
					$this->_redirect("/".$locale_code."/");
				}
			}
		}
	}

}
